from fastapi.security.oauth2 import OAuth2PasswordBearer


from fastapi import FastAPI, Depends
from fastapi.security import OAuth2PasswordBearer
from app.api.v1 import api_router
from app.config.settings import settings
from app.middleware.logging_middleware import LoggingMiddleware
from app.middleware.request_id_middleware import RequestIdMiddleware
from app.middleware.auth_middleware import AuthMiddleware
from app.middleware.exception_middleware import ExceptionMiddleware
from app.middleware.cors_middleware import setup_cors  # 导入CORS中间件
from app.utils.logger import logger
from app.utils.responses import success_response

app = FastAPI(title=settings.APP_NAME, version=settings.APP_VERSION)


# 添加日志中间件
app.add_middleware(LoggingMiddleware)

# 添加请求ID中间件（必须在最前面）
app.add_middleware(RequestIdMiddleware)

# 添加认证中间件
app.add_middleware(AuthMiddleware)

# 添加异常处理中间件
app.add_middleware(ExceptionMiddleware)

# 设置CORS中间件，解决跨域问题
setup_cors(app)

# OAuth2密码流
oauth2_scheme: OAuth2PasswordBearer = OAuth2PasswordBearer(tokenUrl="api/v1/auth/login")

# 包含v1版本的路由
app.include_router(api_router, prefix=settings.API_V1_STR)

@app.get("/")
async def root():
    logger.info("Root endpoint accessed")
    return success_response(message="Welcome to Myth Academy API")